home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-STLport.exe / {app} / Samples / common / src / CEGuiSample.cpp < prev    next >
C/C++ Source or Header  |  2005-11-23  |  8KB  |  262 lines

  1. /************************************************************************
  2.     filename:   CEGuiSample.cpp
  3.     created:    24/9/2004
  4.     author:     Paul D Turner
  5. *************************************************************************/
  6. /*************************************************************************
  7.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  8.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  9.  
  10.     This library is free software; you can redistribute it and/or
  11.     modify it under the terms of the GNU Lesser General Public
  12.     License as published by the Free Software Foundation; either
  13.     version 2.1 of the License, or (at your option) any later version.
  14.  
  15.     This library is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.     Lesser General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU Lesser General Public
  21.     License along with this library; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. *************************************************************************/
  24. #include "CEGuiSample.h"
  25.  
  26. #ifdef HAVE_CONFIG_H
  27. #   include "config.h"
  28. #endif
  29. #include "CEGUIConfig.h"
  30.  
  31. // includes for renderer selector classes
  32. #if defined( __WIN32__ ) || defined( _WIN32 )
  33. #   include "Win32CEGuiRendererSelector.h"
  34. #elif defined(__linux__)
  35. #   ifdef CEGUI_SAMPLES_USE_GTK2
  36. #       include "GTK2CEGuiRendererSelector.h"
  37. #   else
  38. #       include "CLICEGuiRendererSelector.h"
  39. #   endif
  40. #elif defined(__APPLE__)
  41. #   include "MacCEGuiRendererSelector.h"
  42. #endif
  43.  
  44. // includes for application types
  45. #ifdef CEGUI_SAMPLES_USE_OGRE
  46. #   include "CEGuiOgreBaseApplication.h"
  47. #endif
  48. #ifdef CEGUI_SAMPLES_USE_OPENGL
  49. #   include "CEGuiOpenGLBaseApplication.h"
  50. #endif
  51. #ifdef CEGUI_SAMPLES_USE_IRRLICHT
  52. #   include "CEGuiIrrlichtBaseApplication.h"
  53. #endif
  54. #if defined( __WIN32__ ) || defined( _WIN32 )
  55. #   ifdef CEGUI_SAMPLES_USE_DIRECTX_8
  56. #       include "CEGuiD3D81BaseApplication.h"
  57. #   endif
  58. #   ifdef CEGUI_SAMPLES_USE_DIRECTX_9
  59. #       include "CEGuiD3D9BaseApplication.h"
  60. #   endif
  61. #endif
  62. // now we include the base CEGuiBaseApplication just in case someone has managed to
  63. // get this far without any of the renderers.  This ensures the framework will build,
  64. // although there will be no renderers available for selection in the samples.
  65. #include "CEGuiBaseApplication.h"
  66.  
  67. // Include iostream if not on windows.
  68. #if defined( __WIN32__ ) || defined( _WIN32 )
  69. #else
  70. #    include <iostream>
  71. #endif
  72.  
  73.  
  74. /*************************************************************************
  75.     Constructor
  76. *************************************************************************/
  77. CEGuiSample::CEGuiSample() :
  78.         d_rendererSelector(0),
  79.         d_sampleApp(0)
  80. {}
  81.  
  82.  
  83. /*************************************************************************
  84.     Destructor
  85. *************************************************************************/
  86. CEGuiSample::~CEGuiSample()
  87. {
  88.     if (d_sampleApp)
  89.     {
  90.         d_sampleApp->cleanup();
  91.         delete d_sampleApp;
  92.     }
  93.  
  94.     if (d_rendererSelector)
  95.     {
  96.         delete d_rendererSelector;
  97.     }
  98.  
  99. }
  100.  
  101.  
  102. /*************************************************************************
  103.     Application entry point
  104. *************************************************************************/
  105. int CEGuiSample::run()
  106. {
  107.     try
  108.     {
  109.         if (initialise())
  110.             cleanup();
  111.     }
  112.     catch (CEGUI::Exception& exc)
  113.     {
  114.         outputExceptionMessage(exc.getMessage().c_str());
  115.     }
  116.     catch (std::exception& exc)
  117.     {
  118.         outputExceptionMessage(exc.what());
  119.     }
  120.     catch(...)
  121.     {
  122.         outputExceptionMessage("Unknown exception was caught!");
  123.     }
  124.  
  125.     return 0;
  126. }
  127.  
  128.  
  129. /*************************************************************************
  130.     Initialise the sample application
  131. *************************************************************************/
  132. bool CEGuiSample::initialise()
  133. {
  134.     // Setup renderer selection dialog for Win32
  135. #if defined( __WIN32__ ) || defined( _WIN32 )
  136.     d_rendererSelector = new Win32CEGuiRendererSelector;
  137.  
  138.     // enable renderer types supported for Win32
  139. #ifdef CEGUI_SAMPLES_USE_DIRECTX_8
  140.     d_rendererSelector->setRendererAvailability(Direct3D81GuiRendererType);
  141. #endif
  142. #ifdef CEGUI_SAMPLES_USE_DIRECTX_9
  143.     d_rendererSelector->setRendererAvailability(Direct3D9GuiRendererType);
  144. #endif
  145.  
  146. #elif defined(__linux__)
  147.     // decide which method to use for renderer selection
  148. #   ifdef CEGUI_SAMPLES_USE_GTK2
  149.         d_rendererSelector = new GTK2CEGuiRendererSelector();
  150. #   else
  151.         d_rendererSelector = new CLICEGuiRendererSelector();
  152. #   endif
  153.  
  154. #elif defined(__APPLE__)
  155.      d_rendererSelector = new MacCEGuiRendererSelector();
  156. #endif
  157.  
  158.     // enable available renderer types
  159. #ifdef CEGUI_SAMPLES_USE_OGRE
  160.     d_rendererSelector->setRendererAvailability(OgreGuiRendererType);
  161. #endif
  162. #ifdef CEGUI_SAMPLES_USE_OPENGL
  163.     d_rendererSelector->setRendererAvailability(OpenGLGuiRendererType);
  164. #endif
  165. #ifdef CEGUI_SAMPLES_USE_IRRLICHT
  166.     d_rendererSelector->setRendererAvailability(IrrlichtGuiRendererType);
  167. #endif
  168.  
  169.     // get selection from user
  170.     if (d_rendererSelector->inkokeDialog())
  171.     {
  172.         // create appropriate application type based upon users selection
  173.         switch(d_rendererSelector->getSelectedRendererType())
  174.         {
  175. #ifdef CEGUI_SAMPLES_USE_OGRE
  176.         case OgreGuiRendererType:
  177.             d_sampleApp = new CEGuiOgreBaseApplication();
  178.             break;
  179. #endif
  180. #if defined( __WIN32__ ) || defined( _WIN32 )
  181. #ifdef CEGUI_SAMPLES_USE_DIRECTX_8
  182.         case Direct3D81GuiRendererType:
  183.             d_sampleApp = new CEGuiD3D81BaseApplication();
  184.             break;
  185. #endif
  186. #ifdef CEGUI_SAMPLES_USE_DIRECTX_9
  187.         case Direct3D9GuiRendererType:
  188.             d_sampleApp = new CEGuiD3D9BaseApplication();
  189.             break;
  190. #endif // DX9
  191. #endif // Win32
  192. #ifdef CEGUI_SAMPLES_USE_OPENGL
  193.         case OpenGLGuiRendererType:
  194.             d_sampleApp = new CEGuiOpenGLBaseApplication();
  195.             break;
  196. #endif
  197. #ifdef CEGUI_SAMPLES_USE_IRRLICHT
  198.         case IrrlichtGuiRendererType:
  199.             d_sampleApp = new CEGuiIrrlichtBaseApplication();
  200.             break;
  201. #endif
  202.  
  203.         default:
  204.             // TODO: Throw exception or something!
  205.             break;
  206.         }
  207.  
  208.         // execute the base application (which sets up the demo via 'this' and runs it.
  209.         if (d_sampleApp->execute(this))
  210.         {
  211.             // signal that app initialised and ran
  212.             return true;
  213.         }
  214.  
  215.         // sample app did not initialise, delete the object.
  216.         delete d_sampleApp;
  217.         d_sampleApp = 0;
  218.     }
  219.  
  220.     // delete renderer selector object
  221.     delete d_rendererSelector;
  222.     d_rendererSelector = 0;
  223.  
  224.     // signal app did not initialise and run.
  225.     return false;
  226. }
  227.  
  228.  
  229. /*************************************************************************
  230.     Cleanup the sample application.
  231. *************************************************************************/
  232. void CEGuiSample::cleanup()
  233. {
  234.     if (d_sampleApp)
  235.     {
  236.         d_sampleApp->cleanup();
  237.         delete d_sampleApp;
  238.         d_sampleApp = 0;
  239.     }
  240.  
  241.     if (d_rendererSelector)
  242.     {
  243.         delete d_rendererSelector;
  244.         d_rendererSelector = 0;
  245.     }
  246.  
  247. }
  248.  
  249.  
  250. /*************************************************************************
  251.     Output a message to the user in some OS independant way.
  252. *************************************************************************/
  253. void CEGuiSample::outputExceptionMessage(const char* message) const
  254. {
  255. #if defined(__WIN32__) || defined(_WIN32)
  256.     MessageBoxA(0, message, "CEGUI - Exception", MB_OK|MB_ICONERROR);
  257. #else
  258.     std::cout << "An exception was thrown within the sample framework:" << std::endl;
  259.     std::cout << message << std::endl;
  260. #endif
  261. }
  262.